<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box {
            width: 200px;
            height: 200px;
            margin: 100px;
            background-color: pink;
        }
        .son{
            width: 100px;
            height: 100px;
            margin-left: 50px;
            background-color: rgb(255,241,0);
            padding: 10px;
        }
    </style>
</head>
<body>
    <div class="box">
        <!-- <div class="son"></div> -->
    </div>
    <script>
        //获得元素距离带有定位父元素的位置
        //获得元素自身的大小
        //返回的数值不带单位
        var father = document.querySelector('.box');
        // var son =document.querySelector('.son');
        console.log(father.offsetLeft);
        
        // console.log(son.offsetLeft);//父盒子没定位，则返回距body左边的距离

        //还能获取高度和宽度
        // //大小是包含padding+border+width
        // console.log(son.offsetWidth);
        // console.log(son.offsetHeight);

        //offsetParent返回带定位的父亲，否则返回body
        // console.log(son.offsetParent);
        //而parentNode返回的是最近一级的父亲，无论有无定位
        
        father.addEventListener('mousemove',function(e){
            var x=e.pageX-this.offsetLeft;
            var y=e.pageY-this.offsetTop;
            father.innerHTML='x坐标是'+x+'<br>'+'y坐标是'+y;
        })
    </script>
</body>
</html>